package edu.ccut.saturn.basecomponent.databasecomponent;

import edu.ccut.saturn.basecomponent.database.ISaturnSQL;
import edu.ccut.saturn.basecomponent.database.ISaturnSQLBuilder;
import edu.ccut.saturn.basecomponent.database.IUpdateSQLBuilder;
import edu.ccut.saturn.basecomponent.database.SaturnSQLBuilderFactory;
import edu.ccut.saturn.basecomponent.database.SaturnSQLHandler;
import edu.ccut.saturn.basecomponent.database.impl.db2.DB2Attribute;
import edu.ccut.saturn.basecomponent.database.util.SaturnDataUtil;
import edu.ccut.saturn.component.DefaultComponent;
import edu.ccut.saturn.component.IDataItem;
import edu.ccut.saturn.component.IParameterContext;
import edu.ccut.saturn.component.SaturnComponentException;
import edu.ccut.saturn.component.SaturnData;

public class UpdateByPrimaryKey extends DefaultComponent {

	@Override
	public void execute(IParameterContext parameterContext) throws Exception {

		debug(Messages.getString("UpdateByPrimaryKey.18"), parameterContext); //$NON-NLS-1$

		super.execute(parameterContext);

		SaturnData saturnData = (SaturnData) parameterContext.getParameterValue(0);

		String specalColumn = (String) parameterContext.getParameterValue(2);

		String dbType = SaturnDataUtil.getStandardDBType(saturnData);

		ISaturnSQLBuilder builder = SaturnSQLBuilderFactory
				.getSaturnSQLBuilder(dbType);

		if (builder != null) {
			
			String condition = getConditionWithPrimaryKeyByDbType(saturnData);

			IUpdateSQLBuilder sqlBuilder = builder
					.getUpdateSQLBuilder(getBuilderType(saturnData));

			ISaturnSQL saturnSQL = sqlBuilder.build(saturnData, condition,
					specalColumn);

			int count = SaturnSQLHandler.executeUpdate(saturnSQL,
					parameterContext.getConnection());

			parameterContext.setParameter(1, count);
		}
	}

	private String getConditionWithPrimaryKeyByDbType(SaturnData saturnData)
			throws SaturnComponentException {

		if (saturnData == null) {
			throw new SaturnComponentException("The SaturnData is null");
		}

		IDataItem dataItem = SaturnDataUtil.getPKDataItem(saturnData);

		if (dataItem == null) {
			throw new SaturnComponentException(
					"Can not find primaryKey in SaturnData name is"
							+ saturnData.getSaturnDataInfo().getDataFullKey());
		}

		Object primaryKeyValue = saturnData.get(dataItem.getName());

		if (primaryKeyValue == null) {
			throw new SaturnComponentException(
					"PrimaryKeyValue can not be null !");
		}

		String dbType = SaturnDataUtil.getDBType(saturnData);
		String type = dataItem.getType();
		String result = null;
		if (dbType.equals(SaturnSQLBuilderFactory.DB2)) {
			result = " WHERE " + dataItem.getColumn() + " = "
					+ DB2Attribute.quote(type) + primaryKeyValue
					+ DB2Attribute.quote(type);
		} else {
			result = " WHERE " + dataItem.getColumn() + " ='" + primaryKeyValue
					+ "' ";
		}
		return result;
	}

	private String getBuilderType(SaturnData saturnData) {

		if (SaturnDataUtil.hasLOB(saturnData)) {
			return ISaturnSQL.UPDATE_LOB_OPERATION;
		} else {
			return ISaturnSQL.UPDATE_OPERATION;
		}
	}
}
